import requests
from lxml import etree
import json

base_url = "https://bbs.itheima.com/forum-425-1.html"
response = requests.get(base_url)
# 编码方式
response.encoding = "utf-8"
# 解析网页
node = etree.HTML(response.text)
# 标题
titles = node.xpath("//th[@class='new forumtit']/a[@class='s xst']/text()")
# 发帖人
author = node.xpath("//th[@class='new forumtit']/div[@class='foruminfo']/i[@class='z']/a/span/text()")
dates = node.xpath("//th[@class='new forumtit']/div[@class='foruminfo']/i[@class='z']/span[1]/text()")
print(titles)
print(author)
print(dates)
items = []
for i in range(len(titles)):
    d = {}
    d["标题"] = titles[i]
    d["作者"] = author[i]
    d["时间"] = str(dates[i]).strip("@ ")
    items.append(d)
print(items)
with open("论坛信息/"+"1.json","w+",encoding="utf-8") as f:
    f.write(json.dumps(items,ensure_ascii=False,indent=2))